package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.util.PageList;
import com.frogs.framework.util.PageTurn;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.util.date.DateUtil;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.UserFeedbackDao;
import com.frogs.ucenter.entity.UserFeedback;
import com.frogs.ucenter.v1.request.UserFeedbackAddRequest;
import com.frogs.ucenter.v1.request.UserFeedbackListRequest;
import com.frogs.ucenter.v1.request.UserFeedbackUpdateRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.PageListResponse;
import com.frogs.ucenter.v1.response.UpdateResponse;
import com.frogs.ucenter.v1.service.UserFeedbackService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Service(ApiConstants.VERSION+"userFeedbackService")
public class UserFeedbackServiceImpl implements UserFeedbackService {

	private static final Logger log = LoggerFactory.getLogger(UserFeedbackServiceImpl.class);

	@Resource
    private UserFeedbackDao userFeedbackDao;

    /**
	 * 用户评论添加
	 */
	@Override
	public Response insert(Request request) throws Exception  {
		AddResponse<UserFeedback> response = new AddResponse<UserFeedback>();
		try {
			UserFeedbackAddRequest userFeedbackAddRequest = (UserFeedbackAddRequest) request;
			
			UserFeedback userFeedback = new UserFeedback();
			userFeedback.setContent(userFeedbackAddRequest.getContent());
			userFeedback.setEmail(userFeedbackAddRequest.getEmail());
			userFeedback.setPhone(userFeedbackAddRequest.getPhone());
			userFeedback.setUserName(userFeedbackAddRequest.getUserName());
			userFeedback.setReferUrl(userFeedbackAddRequest.getReferUrl());
			userFeedback.setSex(userFeedbackAddRequest.getSex());
			userFeedbackDao.insert(userFeedback);
			
			response.setCode(ApiResultEnum.SUCCESS.getId());
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
	
	/**
	 * 客服记录处理结果
	 */
	@Override
	public Response update(Request request)  throws Exception {
		UpdateResponse<UserFeedback> response = new UpdateResponse<UserFeedback>();
		try {
			UserFeedbackUpdateRequest userFeedbackUpdateRequest = (UserFeedbackUpdateRequest) request;
			
			UserFeedback userFeedback = userFeedbackDao.selectById(userFeedbackUpdateRequest.getId());
			userFeedback.setResult(userFeedbackUpdateRequest.getResult());
			userFeedback.setStatus(userFeedbackUpdateRequest.getStatus());
			userFeedback.setUpdateTime(new Date());
			userFeedback.setAdminId(userFeedbackUpdateRequest.getAdminId());
			userFeedback.setAdminName(userFeedbackUpdateRequest.getAdminName());
			userFeedbackDao.update(userFeedback);
			
			response.setCode(ApiResultEnum.SUCCESS.getId());
		} catch (Exception e) {
			log.error("func[update] request[" + request + "] error[" + e.getMessage() + "]", e);
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
	
	/**
	 * 客服记录列表
	 */
	@Override
	public Response getList(Request request)  throws Exception {
		PageListResponse<UserFeedback> response = new PageListResponse<UserFeedback>();
		try {
			UserFeedbackListRequest goodsFavoriteListRequest = (UserFeedbackListRequest) request;
			String endTime = goodsFavoriteListRequest.getEndTime();
			String startTime = goodsFavoriteListRequest.getStartTime();
			Integer status = goodsFavoriteListRequest.getStatus();
			Integer pageNo = goodsFavoriteListRequest.getPageNum();
			Integer pageSize = goodsFavoriteListRequest.getNumPerPage();
			
			Map<String, Object> params = new HashMap<String, Object>();
			if(status > -1)
				params.put("eqStatus", status);
			
			if (!StringUtil.isEmpty(startTime)) 
				params.put("gtCreateTime",DateUtil.parseDate(startTime + " 00:00:00","yyyyMMdd HH:mm:ss"));
			
			if (!StringUtil.isEmpty(endTime)) 
				params.put("itCreateTime",DateUtil.parseDate(endTime + " 23:59:59","yyyyMMdd HH:mm:ss"));
			
			PageList<UserFeedback> list = userFeedbackDao.selectByMap(params, pageNo, pageSize);
			if (list != null && list.size() > 0) {
				PageTurn pageTurn = list.getPageTurn();
				response.setPageTurn(pageTurn);
				response.setCode(ApiResultEnum.SUCCESS.getId());
				response.setList(list);
			}
		} catch (Exception e) {
			log.error("func[getList] request[" + request + "] error[" + e.getMessage() + "]", e);
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		
		return response;
	}
}